<?php
namespace data\sys\power;
use \data\BaseData;
/**
 * 角色权限数据对象
 */
class RoleMenuData extends BaseData{
    
    
    /**
     * 获取角色拥有的所有权限节点
     */
    public function getPowerList($roleid){
        $where['r.roleid'] = $roleid;
        $where['r.status'] = 1;
        $list =  M('sys_power_role_menu')->alias('r')->join('sys_power_menu m','r.menuid = m.id')
        ->where($where)->field('m.menuurl')->select();
        return $list;
    }
    
    /**
     * 读取某个角色所有的权限
     */
    public function getList($param){
        $fields = ['roleid','status'];
        $where = $this->parseWhere($fields,$param);
        $list =  M('sys_power_role_menu')->where($where)->select();
        return $list;
    }
    
    /**
     * 添加节点
     */
    public function add($roleid,$menuids){
        if( !$menuids )  return ;
        foreach( $menuids as $val){
            $save[] = [
                'menuid'    => $val,
                'roleid'    => $roleid,
                'status'    => 1
            ];
        }
        $result =  M('sys_power_role_menu')->insertAll($save);
        if( !$result ) dataE('更新权限新增失败');
        return true;
    }
    
    /**
     * 删除节点
     */
    public function editStatus($roleid,$menuids,$status){
        if( !in_array( $status, [1,2]) ) dataE('状态超出合法范围');
        if(!$menuids ) return ;
        $where = [
            'menuid'    => is_array($menuids)? ['in',$menuids] : $menuids,
            'roleid'    => $roleid,
        ];
        $result =  M('sys_power_role_menu')->where($where)->update(['status'=>$status]);
        if($result === false) dataE('更新权限删除失败');
        return true;
    }
    
    
}